package com.pactera.madp.cp.controller.oms.medium;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.common.security.annotation.Inner;
import com.pactera.madp.common.security.util.SecurityUtils;
import com.pactera.madp.cp.api.dto.medium.MediumAddDTO;
import com.pactera.madp.cp.api.dto.medium.MediumUpdateDTO;
import com.pactera.madp.cp.api.entity.CorpEntity;
import com.pactera.madp.cp.api.entity.medium.MediumEnyity;
import com.pactera.madp.cp.api.vo.ParseLinkArticleVO;
import com.pactera.madp.cp.api.vo.medium.MediumTypeVO;
import com.pactera.madp.cp.api.vo.medium.MediumVO;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.service.ICorpService;
import com.pactera.madp.cp.service.IMediumService;
import com.pactera.madp.cp.service.ITaskPublishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @description:话术库
 * @author: Huayu
 * @time: 2020/12/4 18:41
 */

@RestController
@AllArgsConstructor
@RequestMapping("/medium")
@Api(value = "medium", tags = "话术库")
public class MediumController {

    private IMediumService mediumServiceImpl;
    private ICorpService corpService;
    private ITaskPublishService taskPublishService;

    /**
     *
     * @description: 查看
     * @return:
     * @author: Huayu
     * @time: 2020/12/6 10:59
     */
    @GetMapping(value="/{id}")
    @ApiOperation(value = "查看话术库素材")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value="话术素材id",dataType ="Integer")
    })
    public R<MediumVO> mediumShow(@PathVariable Integer id){
        return R.ok(mediumServiceImpl.mediumShow(id));
    }

    @GetMapping("/dept")
    @ApiOperation(value = "通过部门id查询话术")
    @ApiImplicitParams({
            @ApiImplicitParam(name ="id",value = "部门id")
    })
    public R<List<MediumEnyity>> mediumListByDept(@RequestParam Integer id){
      return R.ok(mediumServiceImpl.mediumListByDept(id));
    }

    /**
     *
     * @description: 列表
     * @return:
     * @author: Huayu
     * @time: 2020/12/7 13:21
     */
    @GetMapping("/page")
    @ApiOperation(value = "话术库列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="mediumGroupId",value="话术素材组id",dataType ="String"),
            @ApiImplicitParam(name="searchStr",value="搜索内容",dataType ="String"),
            @ApiImplicitParam(name="type",value="话术素材类型。type=0  文本：type=1  图片：type=2  图文：type=3  PDF：type=4  视频：type=5  小程序：type=6  复合消息：type=7",dataType ="Integer")
    })
    public R<Page<List<MediumVO>>> mediumIndex(String mediumGroupId, String searchStr, Integer type, Page page){
        return R.ok(mediumServiceImpl.mediumPage(mediumGroupId, searchStr, type, page));
    }

    @GetMapping("/type")
    @ApiOperation(value = "按type查询话术库列表")
    public R<Page<List<MediumTypeVO>>> mediumType(Integer type, Page page, Integer mediumGroupId, String search){
        return R.ok(mediumServiceImpl.mediumPageByType(type,page,mediumGroupId,search));
    }

    /**
     *
     * @description: 添加素材库
     * @return:
     * @author: Huayu
     * @time: 2020/12/7 14:23
     */
    @SysLog("新建话术库素材")
    @PostMapping
    @ApiOperation(value = "新建话术库素材")
    public R<Boolean> mediumStore(@RequestBody MediumAddDTO dto) {
        dto.setUserName(SecurityUtils.getUser().getUsername());
//        return mediumServiceImpl.insertAllBatch(dto) ? R.ok(Boolean.TRUE,"新增成功") : R.failed(Boolean.FALSE,"新增失败");
        return mediumServiceImpl.insert(dto) ? R.ok(Boolean.TRUE,"新增成功") : R.failed(Boolean.FALSE,"新增失败");
    }

    @SysLog("新建话术库-欢迎语")
    @PostMapping("/greeting")
    @ApiOperation(value = "新建话术库-欢迎语")
    public R mediumAdd(@RequestBody MediumAddDTO dto){
        dto.setUserName(SecurityUtils.getUser().getUsername());
        return mediumServiceImpl.insertByGreeting(dto);
    }

    /**
     *
     * @description:删除
     * @return:
     * @author: Huayu
     * @time: 2020/12/7 14:46
     */
    @SysLog("删除话术库素材")
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除话术库素材")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value="话术素材id",dataType ="Integer")
    })
    public  R<Boolean> mediumDestroy(@PathVariable Integer id) {
        return mediumServiceImpl.deleteMedium(id) ? R.ok(Boolean.TRUE, "删除成功") : R.failed(Boolean.FALSE, "删除失败");
    }

    /**
     *
     * @description: 移动分组
     * @return:
     * @author: Huayu
     * @time: 2020/12/7 15:00
     */
    @PutMapping("/group-update")
    @ApiOperation(value = "移动话术库素材分组")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value="话术素材id",dataType ="Integer"),
            @ApiImplicitParam(name="mediumGroupId",value="新话术素材组id",dataType ="Integer")
    })
    public R<Boolean> mediumGroupUpdate(Integer id,Integer mediumGroupId){
        if(id == null) {
            return R.failed(Boolean.FALSE,"素材ID不能为空");
        }
        if(mediumGroupId == null) {
            return R.failed(Boolean.FALSE,"素材分组ID不能为空");
        }
        MediumEnyity mediumEnyity = new MediumEnyity();
        mediumEnyity.setId(id);
        mediumEnyity.setMediumGroupId(mediumGroupId);
        return mediumServiceImpl.updateMediumById(mediumEnyity) ? R.ok(Boolean.TRUE,"移动成功"):R.failed(Boolean.FALSE,"移动失败");
    }

    /**
     *
     * @description: 修改
     * @return:
     * @author: Huayu
     * @time: 2020/12/7 15:12
     */
    @SysLog("修改话术库素材")
    @PutMapping
    @ApiOperation(value = "修改话术库素材")
    public R<Boolean> mediumUpdate(@RequestBody MediumUpdateDTO dto) {
        dto.setUserName(SecurityUtils.getUser().getUsername());
//        return mediumServiceImpl.updateMediumByIds(dto) ? R.ok(Boolean.TRUE, "修改成功") : R.failed(Boolean.FALSE, "修改失败");
        return mediumServiceImpl.update(dto) ? R.ok(Boolean.TRUE, "修改成功") : R.failed(Boolean.FALSE, "修改失败");
    }

    @GetMapping("/parse-link-article")
    @ApiOperation(value = "解析链接")
    public R<ParseLinkArticleVO> parseLinkArticle(@RequestParam("url")String url){
        return R.ok(mediumServiceImpl.getParseLinkArticle(url));
    }

    @SysLog("保存mediaId(素材id)")
    @Inner
    @GetMapping("/save-media-id")
    @ApiOperation(value = "保存mediaId")
    public void mediumSaveMediaId() {
        List<CorpEntity> list = corpService.list();
        list.forEach(l -> {
            CorpContextHolder.setCorpId(l.getCorpId());
            setMediumService();
            taskPublishService.saveMediaId();
        });
    }

    /**
     * 话术库临时素材上传异步中转
     */
    @Async
    void setMediumService(){
        mediumServiceImpl.saveMediumMediaId();
    }

}
